package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.HttpRequestHelper;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Schedule;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController("ApiScheduleController")
@RequestMapping("/api/hosp")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

//    /schedule/remove
    @ApiOperation("删除排班数据")
    @PostMapping("/schedule/remove")
    public Result remove(HttpServletRequest request){
        Map<String, Object> ScheduleParams = HttpRequestHelper.switchMap(request.getParameterMap());
//        验证signKey
        String sign = (String) ScheduleParams.get("sign");
        String encrypt = MD5.encrypt(sign);
        if (StringUtils.isEmpty(sign) && StringUtils.isEmpty(encrypt) && !encrypt.equals(sign)){
            throw new YyghException(20001,"删除失败");
        }

        scheduleService.remove(ScheduleParams);
        return Result.ok();
    }

    @ApiOperation("保存排班的JSON数据")
    @PostMapping("/saveSchedule")
    public Result<Schedule> saveSchedule(HttpServletRequest request){
        Map<String, Object> ScheduleParams = HttpRequestHelper.switchMap(request.getParameterMap());


//        验证signKey
        String sign = (String) ScheduleParams.get("sign");
        String encrypt = MD5.encrypt(sign);
        if (StringUtils.isEmpty(sign) && StringUtils.isEmpty(encrypt) && !encrypt.equals(sign)){
            throw new YyghException(20001,"保存失败");
        }
        scheduleService.saveSchedule(ScheduleParams);
        return Result.ok();
    }

    @ApiOperation("排班分页查询")
    @PostMapping("/schedule/list")
    public Result<Page> getScheduleList(HttpServletRequest request){
        Map<String, Object> ScheduleParams = HttpRequestHelper.switchMap(request.getParameterMap());
//        验证signKey
        String sign = (String) ScheduleParams.get("sign");
        String encrypt = MD5.encrypt(sign);
        if (StringUtils.isEmpty(sign) && StringUtils.isEmpty(encrypt) && !encrypt.equals(sign)){
            throw new YyghException(20001,"查询失败");
        }
        Page<Schedule> schedulePage = scheduleService.getSchedulePage(ScheduleParams);
        return  Result.ok(schedulePage);
    }


}
